﻿using Engine;
using Game;
using GameEntitySystem;
using System.Collections.Generic;
using TemplatesDatabase;

public class RedFurnaceBlockBehavior : SubsystemBlockBehavior
{
    public SubsystemParticles m_subsystemParticles;

    public SubsystemExplosions m_subsystemExplosions;

    public Dictionary<Point3, FireParticleSystem> m_particleSystemsByCell = new Dictionary<Point3, FireParticleSystem>();

    public Game.Random m_random = new Game.Random();

    public override int[] HandledBlocks => new int[1]
    {
            608
    };

    public override void OnBlockAdded(int value, int oldValue, int x, int y, int z)
    {
        if (Terrain.ExtractContents(value) == 608)
        {
            AddFire(value, x, y, z);
        }
    }

    public override void OnBlockRemoved(int value, int newValue, int x, int y, int z)
    {
        if (Terrain.ExtractContents(value) == 608)
        {
            RemoveFire(x, y, z);
        }
    }

    public override void OnBlockGenerated(int value, int x, int y, int z, bool isLoaded)
    {
        if (Terrain.ExtractContents(value) == 608)
        {
            AddFire(value, x, y, z);
        }
    }

    public override bool OnInteract(TerrainRaycastResult raycastResult, ComponentMiner componentMiner)
    {
        int activeBlockValue = componentMiner.ActiveBlockValue;
        int num66 = Terrain.ExtractContents(activeBlockValue);
        NK9 hL = componentMiner.ComponentPlayer.Entity.FindComponent<NK9>();
        Vector3 Position = new Vector3(raycastResult.CellFace.X, raycastResult.CellFace.Y, raycastResult.CellFace.Z);
        if (hL.ID == 596)
        {
            if (num66 == 586)
            {

                if (m_random.Bool(0.3f))
                {
                    componentMiner.ComponentPlayer.ComponentGui.DisplaySmallMessage("炼制成功", Color.Yellow, blinking: true, playNotificationSound: false);
                    componentMiner.ComponentPlayer.Project.FindSubsystem<SubsystemPickables>().AddPickable(591, 1, Position, default, default); ;
                }
                else
                {
                    componentMiner.ComponentPlayer.ComponentGui.DisplaySmallMessage("炼制失败", Color.Yellow, blinking: true, playNotificationSound: false);
                    componentMiner.ComponentPlayer.Project.FindSubsystem<SubsystemSky>().MakeLightningStrike(Position);
                }

            }
            else
            {
                componentMiner.ComponentPlayer.ComponentGui.DisplaySmallMessage("请手持炼制物品", Color.Yellow, blinking: true, playNotificationSound: false);
            }

            if (num66 == 587)
            {

                if (m_random.Bool(0.3f))
                {
                    componentMiner.ComponentPlayer.ComponentGui.DisplaySmallMessage("炼制成功", Color.Yellow, blinking: true, playNotificationSound: false);
                    componentMiner.ComponentPlayer.Project.FindSubsystem<SubsystemPickables>().AddPickable(590, 1, Position, default, default); ;
                }
                else
                {
                    componentMiner.ComponentPlayer.ComponentGui.DisplaySmallMessage("炼制失败", Color.Yellow, blinking: true, playNotificationSound: false);
                    componentMiner.ComponentPlayer.Project.FindSubsystem<SubsystemSky>().MakeLightningStrike(Position);
                }

            }
            else
            {
                componentMiner.ComponentPlayer.ComponentGui.DisplaySmallMessage("请手持炼制物品", Color.Yellow, blinking: true, playNotificationSound: false);
            }

            if (num66 == 589)
            {

                if (m_random.Bool(0.3f))
                {
                    componentMiner.ComponentPlayer.ComponentGui.DisplaySmallMessage("炼制成功", Color.Yellow, blinking: true, playNotificationSound: false);
                    componentMiner.ComponentPlayer.Project.FindSubsystem<SubsystemPickables>().AddPickable(592, 1, Position, default, default); ;
                }
                else
                {
                    componentMiner.ComponentPlayer.ComponentGui.DisplaySmallMessage("炼制失败", Color.Yellow, blinking: true, playNotificationSound: false);
                    componentMiner.ComponentPlayer.Project.FindSubsystem<SubsystemSky>().MakeLightningStrike(Position);
                }

            }
            else
            {
                componentMiner.ComponentPlayer.ComponentGui.DisplaySmallMessage("请手持炼制物品", Color.Yellow, blinking: true, playNotificationSound: false);
            }

        }
        else
        {
            componentMiner.ComponentPlayer.ComponentGui.DisplaySmallMessage("请副物品栏放入炼药卷轴", Color.Yellow, blinking: true, playNotificationSound: false);
        }
        return false;
    }

    public override void Load(ValuesDictionary valuesDictionary)
    {
        base.Load(valuesDictionary);
        m_subsystemParticles = Project.FindSubsystem<SubsystemParticles>(throwOnError: true);
        m_subsystemExplosions = Project.FindSubsystem<SubsystemExplosions>(throwOnError: true);
    }

    public void AddFire(int value, int x, int y, int z)
    {
        var v = new Vector3(0.5f, 0.2f, 0.5f);
        float size = 0.15f;
        var fireParticleSystem = new FireParticleSystem(new Vector3(x, y, z) + v, size, 16f);
        m_subsystemParticles.AddParticleSystem(fireParticleSystem);
        m_particleSystemsByCell[new Point3(x, y, z)] = fireParticleSystem;
    }

    public void RemoveFire(int x, int y, int z)
    {
        var key = new Point3(x, y, z);
        FireParticleSystem particleSystem = m_particleSystemsByCell[key];
        m_subsystemParticles.RemoveParticleSystem(particleSystem);
        m_particleSystemsByCell.Remove(key);
    }
}